import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus_ghz/pages/404.dart';
import 'package:flutter_enjoy_plus_ghz/pages/home/NoticeDetail.dart';
import 'package:flutter_enjoy_plus_ghz/pages/house/building_list.dart';
import 'package:flutter_enjoy_plus_ghz/pages/house/house_detail.dart';
import 'package:flutter_enjoy_plus_ghz/pages/house/house_form.dart';
import 'package:flutter_enjoy_plus_ghz/pages/house/house_list.dart';
import 'package:flutter_enjoy_plus_ghz/pages/house/location_list.dart';
import 'package:flutter_enjoy_plus_ghz/pages/house/room_list.dart';
import 'package:flutter_enjoy_plus_ghz/pages/login/index.dart';
import 'package:flutter_enjoy_plus_ghz/pages/profile/index.dart';
import 'package:flutter_enjoy_plus_ghz/pages/tabs_page.dart';
import 'package:flutter_enjoy_plus_ghz/utils/TokenManager.dart';

Route<dynamic>? onGenerateRoute(setting) {
  // 如果跳转了一个没有定义的路由 就会进入onGenerateRoute方法
  // 根据判断 动态生成一个组件
  // profile 的页面有token才可以进入 没有token去登录页
  var token = tokenManager.getToken();
  if (token == "") {
    return MaterialPageRoute(builder: (context) => const LoginPage());
  } else {
    if (setting.name == "/profile") {
      return MaterialPageRoute(
          builder: (context) => ProfilePage(
                userInfo: setting.arguments as Map<String, dynamic>,
              ));
    }
    if (setting.name == '/house') {
      return MaterialPageRoute(builder: (context) => const HouseList());
    } else if (setting.name == '/add_house') {
      return MaterialPageRoute(builder: (context) => const LocationList());
    } else if (setting.name == '/building_list') {
      return MaterialPageRoute(
          builder: (context) => BuildingList(
                point: (setting.arguments as Map<String, dynamic>)["point"]
                    as String,
              ));
    } else if (setting.name == '/room_list') {
      return MaterialPageRoute(
          builder: (context) => RoomList(
              point: (setting.arguments as Map<String, dynamic>)["point"]
                  as String,
              building: (setting.arguments as Map<String, dynamic>)["build"]
                  as String));
    } else if (setting.name == '/house_form') {
      return MaterialPageRoute(
          builder: (context) => HouseForm(
                params: setting.arguments as Map<String, dynamic>,
              ));
    } else if (setting.name == '/house_detail') {
      return MaterialPageRoute(
          builder: (context) => HouseDetail(
                id: (setting.arguments as Map)["id"] as String,
              ));
    }
    return MaterialPageRoute(builder: (context) => const NotFound());
    // 这里要立刻返回
  }
}

Map<String, Widget Function(BuildContext)> routes = {
  "/": (context) => const TabsPage(),
  "/notice_detail": (context) => const NoticeDetail(),
  "/login": (context) => const LoginPage(),
  // "/profile": (context) => const ProfilePage()
  // 默认路由 第一个页面访问哪一个 ability中 loadContent
};
